package com.lancoo.edu.platform.operation.backupRestore.controller;

import com.lancoo.edu.platform.operation.backupRestore.service.bo.BrRestoreBoService;
import com.lancoo.edu.platform.operation.commons.annotation.CheckToken;
import com.lancoo.edu.platform.operation.commons.controller.base.BaseResultController;
import com.lancoo.edu.platform.operation.commons.dto.BaseResult;
import lombok.AllArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * @author lzh
 * @Date 2024/4/7 8:51
 * @description 数据恢复控制器
 */
@RestController
@RequestMapping("/api/backupRestore/restore")
@AllArgsConstructor
@Validated
public class BrRestoreController extends BaseResultController {

    private final BrRestoreBoService brRestoreBoService;

    /**
     * 恢复单个系统数据
     *
     * @param id          要恢复的备份记录ID
     * @param userId      执行还原操作的用户ID
     * @return 成功
     */
    @CheckToken
    @PostMapping("/single")
    public BaseResult restoreSingle(@NotNull(message = "要恢复的备份记录ID不能为空") Integer id,
                                    @NotBlank(message = "用户ID不能为空") String userId) {

        brRestoreBoService.restoreSingle(id, userId);
        return success();
    }

    // /**
    //  * 还原指定时间点的全部系统备份数据
    //  *
    //  * @param eduId      教育局ID
    //  * @param backupTime 备份时间点
    //  * @param userId     用户ID
    //  */
    // @CheckToken
    // @PostMapping("/all/byBackupTime")
    // public BaseResult restoreAllByBackupTime(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    //                                          @NotNull(message = "要恢复的备份记录ID不能为空") Date backupTime,
    //                                          @NotNull(message = "教育局ID不能为空") String eduId,
    //                                          @NotBlank(message = "用户ID不能为空") String userId) {
    //     brRestoreBoService.restoreAllByBackupTime(eduId, backupTime, userId);
    //     return success();
    // }

    /**
     * 还原指定备份唯一ID的全部系统备份数据
     *
     * @param eduId       教育局ID
     * @param backupUniqueId 备份唯一ID
     * @param userId         用户ID
     */
    @CheckToken
    @PostMapping("/all/backupUniqueId")
    public BaseResult restoreAllByBackupUniqueId(@Valid @NotNull(message = "要恢复的备份唯一ID不能为空") Long backupUniqueId,
                                                 @NotNull(message = "教育局ID不能为空") String eduId,
                                                 @NotBlank(message = "用户ID不能为空") String userId) {
        brRestoreBoService.restoreAllByBackupUniqueId(eduId, backupUniqueId, userId);
        return success();
    }
}
